<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%@include file="../../common/include-header.jsp" %>
<style>
    .hidden1 {
        display: none;
    }

</style>
<body>
<div class="form-content">
    <form id="form-notice-add" class="form-horizontal">
        <h4 class="form-header h4">基本信息</h4>

        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>年级：</label>
            <div class="col-sm-8">
                <select name="gradeId" id="gradeC" class="form-control">
                    <c:forEach items="${grades}" var="grade">
                        <option value="${grade.id}" ${grade.id==10000 ? "selected":""}>${grade.name}</option>
                    </c:forEach>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>课程：</label>
            <div class="col-sm-8">
                <select id="courseC" name="courseId" class="form-control">
                    <option value="0">请选择</option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>章节：</label>
            <div class="col-sm-8">
                <select id="chapterC" name="chapterId" class="form-control">
                    <option value="0">请选择</option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-xs-3 control-label"><span style="color: red; ">*</span>类型：</label>
            <div class="col-xs-8">
                <label class="radio-box radioLabel">
                    <input name="typeId" type="radio" value="10000" checked>单选
                </label>
                <label class="radio-box radioLabel">
                    <input name="typeId" type="radio" value="10001">多选
                </label>
                <%--                <label class="radio-box">--%>
                <%--                    <input name="typeId" type="radio" value="10002">判断--%>
                <%--                </label>--%>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>试题：</label>
            <div class="col-sm-8">
                <input id="questionC" name="question" autocomplete="off" placeholder="请输入试题" class="form-control"
                       type="text"
                       maxlength="100" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>选项A：</label>
            <div class="col-sm-8">
                <input id="optionA" class="form-control" type="text" name="optionA" id="optionA" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>选项B：</label>
            <div class="col-sm-8">
                <input id="optionB" class="form-control" type="text" name="optionB" id="optionB" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>选项C：</label>
            <div class="col-sm-8">
                <input id="optionC" class="form-control" type="text" name="optionC" id="optionC" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>选项D：</label>
            <div class="col-sm-8">
                <input id="optionD" class="form-control" type="text" name="optionD" id="optionD" required>
            </div>
        </div>

        <div class="form-group radioAnswer">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>单选题答案：</label>
            <div class="col-sm-8" id="radioAnswer">
                <label class="radio-box">
                    <input class="form-control" type="radio" name="answer" value="A">A
                </label>
                <label class="radio-box">
                    <input class="form-control" type="radio" name="answer" value="B">B
                </label>
                <label class="radio-box">
                    <input class="form-control" type="radio" name="answer" value="C">C
                </label>
                <label class="radio-box">
                    <input class="form-control" type="radio" name="answer" value="D">D
                </label>
            </div>
        </div>

        <div class="form-group hidden1 checkboxAnswer">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>多选题答案：</label>
            <div class="col-sm-8" id="checkboxAnswer">
                <label class="check-box">
                    <input class="form-control" type="checkbox" name="answer" value="A">A
                </label>
                <label class="radio-box">
                    <input class="form-control" type="checkbox" name="answer" value="B">B
                </label>
                <label class="radio-box">
                    <input class="form-control" type="checkbox" name="answer" value="C">C
                </label>
                <label class="radio-box">
                    <input class="form-control" type="checkbox" name="answer" value="D">D
                </label>
            </div>
        </div>


        <%--判断的答案--%>
        <%--        <div class="form-group">--%>
        <%--            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>判断题答案：</label>--%>
        <%--            <div class="col-sm-8" id="judgeAnswer">--%>
        <%--                <label class="radio-box">--%>
        <%--                    <input class="form-control" type="radio" value="1" name="judgeAnswer">正确--%>
        <%--                </label>--%>
        <%--                <label class="radio-box">--%>
        <%--                    <input class="form-control" type="radio" value="0" name="judgeAnswer">错误--%>
        <%--                </label>--%>
        <%--            </div>--%>
        <%--        </div>--%>

        <h4 class="form-header h4">试题分析备注</h4>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-3 control-label">内容：</label>
                    <div class="col-xs-8">
                        <textarea id="analysisC" name="analysis" autocomplete="off" maxlength="500" class="form-control"
                                  id="analyse"
                                  rows="3"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<%@include file="../../common/include-footer.jsp" %>

<script src="/static/plugin/select/select2.js"></script>
<script>
    /*function submit1(){
        var gradeId = $('#gradeC').val();
        var courseId = $('#courseC').val();
        var chapterId = $('#chapterC').val();
        var typeId = $("input[name='type']:checked").val();
        var question = $('#questionC').val();
        var analysis = $('#analysisC').val();
        var optionA = null;
        var optionB = null;
        var optionC = null;
        var optionD = null;
        var answer = null;
        var questionTypeId = null;

        if(typeId==1){
            questionTypeId = 10000;
            optionA = $('#optionA').val();
            optionB = $('#optionB').val();
            optionC = $('#optionC').val();
            optionD = $('#optionD').val();
            answer =  $("input[name='radioAnswer']:checked").val();

        }else if(typeId==2){
            questionTypeId = 10001;
            optionA = $('#optionA').val();
            optionB = $('#optionB').val();
            optionC = $('#optionC').val();
            optionD = $('#optionD').val();
            answer = "";
            $.each($('input:checkbox:checked'), function () {
                answer += $(this).val() + '';
            });


        }else if(typeId==4){
            questionTypeId = 10002;
            var pd = $("input[name='judgeAnswer']:checked").val();
            if(pd==1){
                answer = "A";
            }else if(pd==0){
                answer = "B";
            }
        }

        var data = {};
        data.gradeId = gradeId;
        data.courseId = courseId;
        data.chapterId = chapterId;
        data.questionTypeId = questionTypeId;
        data.optionA = optionA;
        data.optionB = optionB;
        data.optionC = optionC;
        data.optionD = optionD;
        data.answer = answer;
        data.question = question;
        data.analysis = analysis;
        $.ajax({
            url:"/question/addQuestions",    //请求的url地址
            dataType:"json",   //返回格式为json
            data:{"gradeId":data.gradeId,"courseId":data.courseId,
            "chapterId":data.chapterId,"typeId":data.questionTypeId,
            "optionA":data.optionA,"optionB":data.optionB,
            "optionC":data.optionC,"optionD":data.optionD,
            "answer":data.answer,"question":data.question,
            "analysis":data.analysis},    //参数值
            type:"POST",   //请求方式
            success:function(result){
                if(result){
                    layer.msg("添加成功!");
                }else {
                    layer.msg("添加失败!原因是="+result.errorMsg)
                }
            }

        });
    }*/
    var data;
    var prefix = "/question";

    $(function () {
        var gradeId = $('#gradeC').val();
        $.ajax({
            url: "${pageContext.request.contextPath}/question/getCourseByGradeId",    //请求的url地址
            dataType: "json",   //返回格式为json
            data: {"gradeId": gradeId},    //参数值
            type: "POST",   //请求方式
            success: function (data) {
                var courses = data;
                for (var i = 0; i < courses.length; i++) {
                    var name = courses[i].name;
                    var id = courses[i].id;
                    //给下拉框动态赋值
                    $("#courseC").append("<option class='hz1' value='" + id + "'>" + name + "</option>");
                }
                // alert($("#subid").val());
            }
        });
        $('#chapterC').attr("disabled", true);

        $('.radioLabel').click(function () {
            var typeId = $(this).find("input").val();
            if (typeId == 10000) {
                $('.radioAnswer').removeClass('hidden1');
                $('.checkboxAnswer').addClass('hidden1');
            } else if (typeId == 10001) {
                $('.radioAnswer').addClass('hidden1');
                $('.checkboxAnswer').removeClass('hidden1');
            }
        });

        $('#gradeC').change(function () {
            var gradeId = $('#gradeC').val();
            $.ajax({
                url: "${pageContext.request.contextPath}/question/getCourseByGradeId",    //请求的url地址
                dataType: "json",   //返回格式为json
                data: {"gradeId": gradeId},    //参数值
                type: "POST",   //请求方式
                success: function (data) {
                    var courses = data;
                    $('.hz1').remove();
                    for (var i = 0; i < courses.length; i++) {
                        var name = courses[i].name;
                        var id = courses[i].id;
                        //给下拉框动态赋值
                        $("#courseC").append("<option selected class='hz1' value='" + id + "'>" + name + "</option>");
                    }
                    // alert($("#subid").val());
                }
            });
            $('#chapterC').attr("disabled", true);
        });

        $('#courseC').change(function () {
            var courseId = $('#courseC').val();
            $.ajax({
                url: "${pageContext.request.contextPath}/question/getChapterByCourseId",    //请求的url地址
                dataType: "json",   //返回格式为json
                data: {"courseId": courseId},    //参数值
                type: "POST",   //请求方式
                success: function (data) {
                    var chapters = data;
                    $('.hz2').remove();
                    for (var i = 0; i < chapters.length; i++) {
                        var name = chapters[i].name;
                        var id = chapters[i].id;
                        //给下拉框动态赋值
                        $("#chapterC").append("<option class='hz2' value='" + id + "'>" + name + "</option>");
                    }
                    // alert($("#subid").val());
                }
            });
            $('#chapterC').attr("disabled", false);
        });
    });


    function checksubmit() {
        var typeId = $("input[name=typeId]:checked").val();
        var question = $("input[name=question]").val();
        var analysis = $("#analysis").val();
        var gradeId = $("#gradeC").val();
        var chapterId = $("#chapterC").val();
        var courseId = $("#courseC").val();
        if (question == "") {
            layer.msg("请输入试题");
            return false;
        }

        //获取ABCD选项
        var optionA = $("input[name='optionA']").val();
        var optionB = $("input[name='optionB']").val();
        var optionC = $("input[name='optionC']").val();
        var optionD = $("input[name='optionD']").val();

        if (optionA == "" || typeId == undefined) {
            layer.msg("请输入选项A");
            return false;
        }
        if (optionB == "" || typeId == undefined) {
            layer.msg("请输入选项B");
            return false;
        }
        if (optionC == "" || typeId == undefined) {
            layer.msg("请输入选项C");
            return false;
        }
        if (optionD == "" || typeId == undefined) {
            layer.msg("请输入选项D");
            return false;
        }

        var answer = "";
        //单选
        if (typeId == 10000) {
            answer = $(".radioAnswer input[name='answer']:checked").val();
            if (answer == "" || answer == undefined) {
                layer.msg("请选择答案");
                return false;
            }
        } else if (typeId == 10001) {
            //多选
            $.each($('input:checkbox:checked'), function () {
                answer += $(this).val();   //如果选中，将value添加到变量s中
            });
            if (answer == "") {
                layer.msg("请选择答案");
                return false;
            }
        }
        /*else if (type == 3) {
            //填空
            var blackAnswer = $("input[name='blackAnswer']").val();
            if(blackAnswer == ""){layer.msg("请输入答案");return false;}

        } else if (type == 4) {
            var judgeAnswer = $("input[name='judgeAnswer']:checked").val();
            if(judgeAnswer == "" || judgeAnswer == undefined){layer.msg("请选择答案");return false;}
            //判断
        } else if (type == 5) {
            //简答
            var shortAnswer = $("#shortAnswer").val();
            if(shortAnswer == ""){layer.msg("请输入答案");return false;}
        }

         */

        data = {
            "typeId": typeId, "question": question, "analysis": analysis, "gradeId": gradeId,
            "chapterId": chapterId, "courseId": courseId, "optionA": optionA, "optionB": optionB,
            "optionC": optionC, "optionD": optionD, "answer": answer
        };

        return true;
    }

    function submitHandler() {
        if (!checksubmit()) {
            return;
        }
        $.operate.save(prefix + "/add", data);
    }
</script>
</body>
</html>
